package com.cxh.rmsq.ui.activity.self

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ToastUtils
import com.cretin.www.cretinautoupdatelibrary.model.DownloadInfo
import com.cretin.www.cretinautoupdatelibrary.utils.AppUpdateUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.common.Constant
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.net.newNet.ProjectViewModel
import com.cxh.rmsq.ui.BaseActivity
import com.cxh.rmsq.ui.activity.WebViewActivity
import com.cxh.rmsq.utils.jumpToAppStore

class AboutUsActivity : BaseActivity(), View.OnClickListener {
    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(ProjectViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_about_us)
        findViewById<ConstraintLayout>(R.id.cl_toolbar).setPadding(
            0,
            BarUtils.getStatusBarHeight(),
            0,
            0
        )
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent))
        BarUtils.setStatusBarLightMode(this, true)

        findViewById<TextView>(R.id.tv_version).text = AppUtils.getAppVersionName()
        findViewById<LinearLayout>(R.id.iv_back).setOnClickListener(this)
        findViewById<LinearLayout>(R.id.userAgreement).setOnClickListener(this)
        findViewById<LinearLayout>(R.id.privacyPolicy).setOnClickListener(this)
        findViewById<LinearLayout>(R.id.behavior).setOnClickListener(this)
        findViewById<LinearLayout>(R.id.ll_update).setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> {
                finish()
            }

            R.id.userAgreement -> {
                startActivity(
                    Intent(
                        this,
                        WebViewActivity::class.java
                    ).putExtra(WebViewActivity.PARAMS_URL, Constant.userAgreement)
                        .putExtra(WebViewActivity.PARAMS_TITLE, "用户协议")
                )
            }

            R.id.privacyPolicy -> {
                startActivity(
                    Intent(
                        this,
                        WebViewActivity::class.java
                    ).putExtra(WebViewActivity.PARAMS_URL, Constant.privacyPolicy)
                        .putExtra(WebViewActivity.PARAMS_TITLE, "隐私政策")
                )
            }

            R.id.behavior -> {
                startActivity(
                    Intent(
                        this,
                        WebViewActivity::class.java
                    ).putExtra(WebViewActivity.PARAMS_URL, Constant.behaviorStandard)
                        .putExtra(WebViewActivity.PARAMS_TITLE, "行为规范")
                )
            }

            R.id.ll_update -> {
                update()
            }
        }
    }

    private fun update() {
        viewModel.getAppVersions(IMManager.getInstance().currentId, "2", Build.MANUFACTURER) {
            if (it.code == 200) {
                if (!TextUtils.isEmpty(it.data.version)) {
//                    var code = versionToInt(it.data.version!!)
                    var code = it.data.version!!.replace(".","").trim().toInt()
                    Log.e("versionVersion", code.toString())
//                    if (s.toInt().compareTo(AppUtils.getAppVersionCode()) == 1) { //a>b
//                    }

                    if (it.data.updateType == 1) {//1直接下载  2 网址下载  3 应用市场
                        var isForce = 0
                        if (it.data.isForce == 1) {//强制更新
                            isForce = 2
                        } else {
                            isForce = 0
                        }
                        //下载apk
                        val info = DownloadInfo().setApkUrl(it.data.downloadUrl)
//                            .setFileSize(31338250)
                            .setProdVersionCode(code)
                            .setProdVersionName(it.data.version)
//                    .setMd5Check("68919BF998C29DA3F5BD2C0346281AC0")
                            .setForceUpdateFlag(isForce)
                            .setUpdateLog(it.data.intro)
                        AppUpdateUtils.getInstance().checkUpdate(info)
                    } else if (it.data.updateType == 3) {
                        jumpToAppStore(this)
                    }
                } else {
                    ToastUtils.showShort("已是最新版本")
                }

            } else {
                com.cxh.rmsq.utils.ToastUtils.showToast(it.msg)
            }

        }
    }

    fun versionToInt(version: String): Int {
        val parts = version.split("\\.".toRegex()).dropLastWhile { it.isEmpty() }
            .toTypedArray()
        val major = if (parts.size > 0) parts[0].toInt() else 0
        val minor = if (parts.size > 1) parts[1].toInt() else 0
        val patch = if (parts.size > 2) parts[2].toInt() else 0
        return major shl 16 or (minor shl 8) or patch
    }
}